<?php 
namespace Gigasweb\Google\Maps;

class Map {
	public $ID;
	public $Class;
	public $Markers;
	public $Center;
	public $Zoom;
	public $FitBounds;
	public $Style;
	
	
	public function __construct( $params ) {
		$defaults = array (
				'id' => rand(),
				'class' => '',
				'center' => array( 'lat' => '41.9229', 'lng' => '12.5153 '),
				'fit_bounds' => true,
				'zoom' => 8,
				'style' => '',
				'marker' => ''
		);
		
		$params = array_merge( $defaults, $params );

		$this->Markers = array();
		$this->ID = $params['id'];
		$this->Class = $params['class'];
		$this->Center = $params['center'];
		$this->Zoom = $params['zoom'];
		$this->FitBounds = $params['fit_bounds'];
		$this->Style = $params['style'];
		$this->Marker = $params['marker'];
	}
	
	public function addMarker(  $lat, $lng, $id = '', $title = '', $callback = '' ) {
		$this->Markers[] = array( 'id' => $id, 'lat' => $lat, 'lng' => $lng, 'title' => $title, 'callback' => $callback );
	}	
	
	public function setStyle( $jsonStyle ) {
		$this->Style = $jsonStyle;
	}
	
	public function render() {
		?>
		<div id="<?php echo $this->ID; ?>" class="<?php echo $this->Class; ?>"></div>
		<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
		<script type="text/javascript" defer="defer" async="async">
			jQuery(window).load(function() {
				var geocoder,map;
				
				function initialize() {
					geocoder = new google.maps.Geocoder();
					var latlng = new google.maps.LatLng( <?php echo $this->Center['lat']; ?>, <?php echo $this->Center['lng']; ?> );
					var markers = new Array();
					var myOptions = {
						zoom: <?php echo $this->Zoom; ?>,
						center: latlng,
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false,
						streetViewControl: false
					};
					map = new google.maps.Map(document.getElementById("<?php echo $this->ID; ?>"), myOptions);
					<?php if( $this->Style ) : ?>
						map.setOptions({styles: <?php echo $this->Style; ?>});
					<?php endif; ?>
					
					<?php
					$maxLng = false;
					$maxLat = false;
					$minLng = false;
					$minLat = false;					
					foreach( $this->Markers as $coord ) {
						$maxLng = $maxLng !== false && ( $maxLng > $coord['lng'] ) ? $maxLng : $coord['lng'];
						$maxLat = $maxLat !== false && ( $maxLng > $coord['lat'] ) ? $maxLng : $coord['lat'];
						$minLng = $minLng !== false && ( $minLng < $coord['lng'] ) ? $minLng : $coord['lng'];
						$minLat = $minLat !== false && ( $minLat < $coord['lat'] ) ? $minLat : $coord['lat'];
						
						$rnd = $coord['id'] ? $coord['id'] : rand();
						?>
						var coord = new google.maps.LatLng(<?php echo $coord['lat']; ?>, <?php echo $coord['lng']; ?>);
						marker<?php echo $rnd ?> = new google.maps.Marker({
							map: map,
							position: coord,
							draggable: false,
							title: '<?php echo $coord['title']; ?>'
							<?php if( $this->Marker ) : ?>
							,icon: '<?php echo $this->Marker; ?>'
							<?php endif; ?>
						});
						markers.push(coord);
						<?php 
						if( $coord['callback'] ) {
							?>
							google.maps.event.addListener(marker<?php echo $rnd ?>, 'click', function() {
								<?php echo $coord['callback']; ?>;
							});
							<?php
						}
					}
					?>

					<?php if( $this->FitBounds ) : ?>
						var latlngbounds = new google.maps.LatLngBounds();
						for (var i = 0; i < markers.length; i++)
						   latlngbounds.extend(markers[i]);
						map.setCenter(latlngbounds.getCenter());
						map.fitBounds(latlngbounds);
					<?php endif; ?>
				}

				initialize();
			});
		</script>
		<?php 
	}
}